﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace sviewer
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        string filepath;
        Point prevMousePos;
        bool isMouseDown = false;
        double zoomRate = 1;
        double zoomRateDelta = 0.001f;
        BitmapImage bitmap;

        public MainWindow()
        {
            InitializeComponent();

            string[] args = Environment.GetCommandLineArgs();

            if (args.Length < 2)
                Close();

            filepath = args[1];

            bitmap = new BitmapImage(new Uri(filepath));
            image.Source = bitmap;

            updateImage();
        }

        private void image_Drop(object sender, DragEventArgs e)
        {

        }

        private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            prevMousePos = PointToScreen(Mouse.GetPosition(this));
            isMouseDown = true;

            if (e.ClickCount >= 2)
                Close();
        }

        private void image_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isMouseDown)
                return;

            Point mousePos = PointToScreen(Mouse.GetPosition(this));

            double dx = mousePos.X - prevMousePos.X;
            double dy = mousePos.Y - prevMousePos.Y;

            //Console.WriteLine(dx + " " + dy);

            Left += dx;
            Top += dy;
            prevMousePos = mousePos;
        }

        private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = false;
        }

        private void image_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            double newZoomRate = zoomRate + e.Delta * zoomRateDelta;
            if (newZoomRate < 0.1) newZoomRate = 0.1;
            if (newZoomRate > 5.0) newZoomRate = 5.0;

            zoomRate = newZoomRate;
            updateImage();
        }

        private void updateImage()
        {
            double width = bitmap.Width * zoomRate;
            double height = bitmap.Height * zoomRate;

            Point mousePos = Mouse.GetPosition(this);

            double dx = -(width * mousePos.X/Width - mousePos.X);
            double dy = -(height* mousePos.Y/Height - mousePos.Y);

            double left = Left + dx;
            double top = Top + dy;

            IntPtr handle = new WindowInteropHelper(this).Handle;

            SetWindowPos(handle, 0, (int)left, (int)top, (int)width, (int)height, 0);
            MoveWindow(handle, (int)left, (int)top, (int)width, (int)height, true);

            //Width = width;
            //Height = height;
            //Left = left;
            //Top = top;

            Console.WriteLine(width + " " + height + ", " + Width + " " + Height);
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    }
}
